﻿using System;
using System.Collections.Generic;
using System.Linq;

using Biz.EF.Platform.Gxt;

using Hjt212Parser;
using log4net;

namespace Biz.Process
{
    /// <summary>
    /// 主要处理统计，日数据统计到月数据，月数据统计到年
    /// </summary>
    public static class ProcessorSummation4Monitor
    {
        private static ILog Log = LogManager.GetLogger(typeof (ProcessorSummation4Monitor));
        /// <summary>
        /// 根据2031（日数据）指令触发月数据累加(在线)
        /// </summary>
        /// <param name="pCtx"></param>
        /// <param name="content"></param>
        public static void Process2031Sum2Month(PlatformGxtContext pCtx, string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                //1、插入或更新主记录
                var begin = new DateTime(datatime.Value.Year, datatime.Value.Month, 1, 0, 0, 0);
                var end = begin.AddMonths(1);
                var xaxis = pCtx.xaxis_month.Where(t => t.MN.Equals(mn))
                    .FirstOrDefault(t => t.datatime >= begin && t.datatime < end);
                if (xaxis == null)
                {
                    xaxis = new xaxis_month();
                    pCtx.xaxis_month.Add(xaxis);
                }
                xaxis.MN = mn;
                xaxis.datatime = datatime;
                pCtx.SaveChanges();
                //2、插入或更新状态
                var state = pCtx.state_month.FirstOrDefault(t => t.MN.Equals(mn));
                if (state == null)
                {
                    state = new state_month();
                    pCtx.state_month.Add(state);
                }
                state.MN = mn;
                state.datatime = datatime.Value;
                state.id = xaxis.id;
                pCtx.SaveChanges();
                //3、插入或更新数据
                var xaxises =
                    pCtx.xaxis_day.Where(t => t.MN.Equals(mn))
                        .Where(t => t.datatime >= begin && t.datatime < end)
                        .ToList();
                List<int> dayIds = xaxises.Select(xaxisDay => xaxisDay.id).ToList();
                if (dayIds.Count != 0)
                {
                    var factors = Parser.GetPollutantCodes(content);
                    foreach (var factor1 in factors)
                    {
                        var factor = factor1;
                        var oldFactor = Convert.GetOldFactorFromNewFactor(factor);
                        if (oldFactor != null)
                        {
                            factor = oldFactor;
                        }
                        var avg = pCtx.data_day.Where(t => dayIds.Contains(t.ID)).Where(t => t.pollutant.Equals(factor)).Average(t => t.avg);
                        var cou = pCtx.data_day.Where(t => dayIds.Contains(t.ID)).Where(t => t.pollutant.Equals(factor)).Sum(t => t.cou);
                        var min = pCtx.data_day.Where(t => dayIds.Contains(t.ID)).Where(t => t.pollutant.Equals(factor)).Min(t => t.min);
                        var max = pCtx.data_day.Where(t => dayIds.Contains(t.ID)).Where(t => t.pollutant.Equals(factor)).Max(t => t.max);

                        if (avg != null && cou != null && min != null && max != null)
                        {
                            data_month data =
                                pCtx.data_month.Where(t => t.ID == xaxis.id).FirstOrDefault(t => t.pollutant.Equals(factor));
                            if (data == null)
                            {
                                data = new data_month();
                                pCtx.data_month.Add(data);
                            }
                            data.ID = xaxis.id;
                            data.pollutant = factor;
                            data.avg = avg.Value;
                            data.cou = cou.Value;
                            data.min = min.Value;
                            data.max = max.Value;
                          
                        }
                    }
                }
                pCtx.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error( ex);
            }
            
           
        }
        /// <summary>
        /// 根据2031（日数据）指令触发年数据累加(在线)
        /// </summary>
        /// <param name="pCtx"></param>
        /// <param name="content"></param>
        public static void Process2031Sum2Year(PlatformGxtContext pCtx, string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                //1、插入或更新主记录
                var begin = new DateTime(datatime.Value.Year, 1, 1, 0, 0, 0);
                var end = begin.AddYears(1);
                var xaxis = pCtx.xaxis_year.Where(t => t.MN.Equals(mn))
                    .FirstOrDefault(t => t.datatime >= begin && t.datatime < end);
                if (xaxis == null)
                {
                    xaxis = new xaxis_year();
                    pCtx.xaxis_year.Add(xaxis);
                }
                xaxis.MN = mn;
                xaxis.datatime = datatime;
                pCtx.SaveChanges();
                //2、插入或更新状态

                var state = pCtx.state_year.FirstOrDefault(t => t.MN.Equals(mn));
                if (state == null)
                {
                    state = new state_year();
                    pCtx.state_year.Add(state);
                }
                state.MN = mn;
                state.datatime = datatime.Value;
                state.id = xaxis.id;
                pCtx.SaveChanges();
                //3、插入或更新数据
                var xaxises =
                    pCtx.xaxis_day.Where(t => t.MN.Equals(mn))
                        .Where(t => t.datatime >= begin && t.datatime < end)
                        .ToList();
                List<int> dayIds = xaxises.Select(xaxisDay => xaxisDay.id).ToList();
                if (dayIds.Count != 0)
                {
                    var factors = Parser.GetPollutantCodes(content);
                    foreach (var factor1 in factors)
                    {
                        var factor = factor1;
                        var oldFactor = Convert.GetOldFactorFromNewFactor(factor);
                        if (oldFactor != null)
                        {
                            factor = oldFactor;
                        }
                        var avg = pCtx.data_day.Where(t => dayIds.Contains(t.ID)).Where(t => t.pollutant.Equals(factor)).Average(t => t.avg);
                        var cou = pCtx.data_day.Where(t => dayIds.Contains(t.ID)).Where(t => t.pollutant.Equals(factor)).Sum(t => t.cou);
                        var min = pCtx.data_day.Where(t => dayIds.Contains(t.ID)).Where(t => t.pollutant.Equals(factor)).Min(t => t.min);
                        var max = pCtx.data_day.Where(t => dayIds.Contains(t.ID)).Where(t => t.pollutant.Equals(factor)).Max(t => t.max);

                        if (avg != null && cou != null && min != null && max != null)
                        {
                            data_year data =
                                pCtx.data_year.Where(t => t.ID == xaxis.id).FirstOrDefault(t => t.pollutant.Equals(factor));
                            if (data == null)
                            {
                                data = new data_year();
                                pCtx.data_year.Add(data);
                            }
                            data.ID = xaxis.id;
                            data.pollutant = factor;
                            data.avg = avg.Value;
                            data.cou = cou.Value;
                            data.min = min.Value;
                            data.max = max.Value;
                        
                        }
                    }
                }
                pCtx.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error( ex);
            }
            
        }

    }
}
